﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using WebVideo.Areas.Admin.Models;
using System.Collections;

namespace WebVideo.Areas.Admin.Models
{
    public class TypeVideoModel
    {
        public int Id { get; set; }

         [Required(ErrorMessage = "Tên không được rỗng")]
         [StringLength(50, ErrorMessage = "Tên phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        
        public string Name { get; set; }

         [Required(ErrorMessage = "Số điểm không được để trống")]
         [RegularExpression("^\\+?\\d+$", ErrorMessage = "Bạn phải nhập số")]
        public int Point { get;set; }

        // load
         public static IEnumerable<EntityTypeVideo> LayDuLieu()
         {
             dbVideoDataContext db = new dbVideoDataContext();
             var query = from p in db.EntityTypeVideos
                         select p;

             return query.ToList();
         }

         public static void Insert(TypeVideoModel Mtnew)
         {
             dbVideoDataContext db = new dbVideoDataContext();
             EntityTypeVideo tnew = new EntityTypeVideo();
             tnew.Name = Mtnew.Name;
             tnew.Point = Mtnew.Point;

             db.EntityTypeVideos.InsertOnSubmit(tnew);
             db.SubmitChanges();

         }


         public static TypeVideoModel  LayModel(int IdTypeVideo)
         {
             TypeVideoModel Mtnew = new TypeVideoModel();


             dbVideoDataContext db = new dbVideoDataContext();

             EntityTypeVideo tnew = db.EntityTypeVideos.Where(p => p.Id == IdTypeVideo).Single();

             Mtnew.Name = tnew.Name;
             Mtnew.Id = tnew.Id;
             Mtnew.Point = (int)tnew.Point;

             return Mtnew;

         }
         public static void Edit(TypeVideoModel Mtnew)
         {
             dbVideoDataContext db = new dbVideoDataContext();

             EntityTypeVideo tnew = db.EntityTypeVideos.Where(p => p.Id == Mtnew.Id).Single();
             tnew.Name = Mtnew.Name;
             tnew.Point = Mtnew.Point;
             db.SubmitChanges();

         }

         public static void Delete(int Id)
         {
             dbVideoDataContext db = new dbVideoDataContext();

             EntityTypeVideo tnew = db.EntityTypeVideos.Where(p => p.Id == Id).Single();
             db.EntityTypeVideos.DeleteOnSubmit(tnew);
             db.SubmitChanges();

         }


    }
}